/// 分类
const category = require("express").Router()
const {rules, valid} = require("../../utils/rules");
const sqlOptions = require("../../sql");
const {listToTree} = require("../../utils");

const nanoid = require("nanoid").nanoid

const categorySave = [
    {
        name: "category_name",
        message: "分类名称"
    },
    {
        name: "category_order",
        message: "排序"
    },
    {
        name: "parent_id",
        message: "分类父id"
    },
]

const categoryUpdate = [].concat(categorySave, [
    {
        name: "category_id",
        message: "分类id"
    }
])

/**
 * sms_category
 */

// 添加分类
category.post("/save", rules(categorySave), valid, async (req, res) => {
    try {
        let category_order;
        let category_name;
        const category_id = nanoid()
        const data = {
            category_name ,
            category_order,
            parent_id
        } = req.body
        const sql = sqlOptions({
            name: 'sms_category',
            data: {
                category_name,
                parent_id
            },
            if: "AND",
            method: 'list',
            select: 'category_name'
        })
        const list = await req.db.query(sql)
        if (list.length) {
            let flag = false
            for (const item of list) {
                flag = item.category_name.includes(category_name)
            }
            if (flag) {
                return res.status(200).send({
                    code: 400,
                    message: '名称重复',
                    data: ''
                })
            }
        }
        const save = sqlOptions({
            method: "save",
            data: {
                ...data,
                category_id
            },
            name: 'sms_category'
        })
        // console.log(save, '添加')
        await req.db.query(save)
        return res.status(200).send({
            code: 200,
            message: '添加成功',
            data: ''
        })
    } catch (e) {
        // console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 编辑
category.post("/update",rules(categoryUpdate), valid, async (req, res) => {
    try {
        const data = {
            category_name ,
            category_order,
            parent_id,
            category_id
        } = req.body

        const sql = sqlOptions({
            method: "update",
            name: 'sms_category',
            key: {
                category_id
            },
            data: {
                category_name ,
                category_order,
            }
        })

        await req.db.query(sql)

        return res.status(200).send({
            code: 200,
            message: '修改成功',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 查询 接口修改
// category.post("/list", async (req, res) => handlerList(req, res, 'sms_category'))
category.get("/queryList", async (req, res) => {
    try {
        const sql = sqlOptions({
            method: "list",
            name: 'sms_category',
            data: {}
        })
        const list = await req.db.query(sql)
        return res.status(200).send({
            code: 200,
            message: "",
            data: list.sort((a,b) => a.category_order - b.category_order)
        })
    } catch (e) {
        // console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 树状查询
category.get("/treeList", async (req, res) => {
    try {
        const sql = sqlOptions({
            name: "sms_category",
            method: "list",
            data: {}
        })
        const list = await req.db.query(sql)
        const data = listToTree(list.map(item => {
            delete item.id
            return item
        }), {
            idKey: "category_id",
            order: "category_order",
            childrenKey: "children",
            parentKey: "parent_id"
        })
        res.status(200).send({
            code: 200,
            message: "",
            data
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 删除
/**
 * 将品牌表, 商品表, 商品推荐表, 分类表全部删除 购物车表全部失效
 */
category.delete("/delete", async (req, res) => {
   try {
       const {category_id} = req.query
       // 分类表
       const categorySql = sqlOptions({
           method: 'delete',
           name: 'sms_category',
           data: {
               category_id
           }
       })
       // 品牌
       const brandSql = sqlOptions({
           method: 'delete',
           name: 'sms_brand',
           data: {
               category_id
           }
       })
       // 商品
       const product = sqlOptions({
           method: 'delete',
           name: 'sms_product',
           data: {
               category_id
           }
       })
       // 商品推荐
       const recommend = sqlOptions({
           method: 'delete',
           name: 'sms_recommend',
           data: {
               category_id
           }
       })
       await req.db.query(categorySql)
       await req.db.query(brandSql)
       await req.db.query(product)
       await req.db.query(recommend)
       return res.status(200).send({
           code: 200,
           message: '删除成功',
           data: ''
       })
   } catch (e) {
       // console.log(e)
       return res.status(500).send({
           code: 500,
           message: '系统错误',
           data: ''
       })
   }

})


module.exports = category
